
package com.ifcar.app.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.activity.R;

public class HeadAdvertismentAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private List<Map<String, Object>> mList;
    private BaseImageDownloader mImageDownloader;
    private Context mContext;
    
    public HeadAdvertismentAdapter(Context context, List<Map<String, Object>> list, BaseImageDownloader imageDownloader) {
        mInflater = LayoutInflater.from(context);
        mList = list;
        mImageDownloader = imageDownloader;
        mContext = context;
    }

    /**
     * 根据position,返回这个位置对应的比较大的中间的位置.
     * 
     * @param position
     * @return
     */
    public int getInitPosition(int position) {
        if (position < 0) {
            return position;
        }

        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }

        position = getRealPosition(position);

        final int count = getCount();

        return count / 2 - (count / 2) % realCount + position;
    }

    /**
     * 返回在列表中的真实position.
     * 
     * @param position
     * @return
     */
    public int getRealPosition(int position) {
        if (position < 0) {
            return position;
        }
        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }
        return position % realCount;
    }

    /**
     * 返回在列表真实count.
     * 
     * @param position
     * @return
     */
    public int getRealCount() {
        return mList.size();
    }
    
    @Override
    public int getCount() {
        return getRealCount();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(getRealPosition(position));
    }

    @Override
    public long getItemId(int position) {
        return getRealPosition(position);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup viewparent) {
        position = getRealPosition(position);
        if (getRealCount() == 0) {
            return null;
        }

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.head_advertisment_item, null);
            
            convertView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
                    Gallery.LayoutParams.FILL_PARENT));
        }
        
        Map<String, Object> map = mList.get(position);
        ImageView imageView = (ImageView)convertView;
        
        String imgsrc = (String)map.get(GetColumnsInfo.IMGSRC_TAG);
        if (!TextUtils.isEmpty(imgsrc)) {
            imgsrc = PicUtil.getHeandBitMapURL(imgsrc, mContext);
        }
        
    	mImageDownloader.setThread(Thread.currentThread());
		mImageDownloader.download(imgsrc, imageView);
			
        return convertView;
    }
    
}
